// import { message } from "@/utils/message";
import { transformI18n } from "@/plugins/i18n";
import { getUserInfo, addRecharge } from "@/api/home";
import addBalanceForm from "../form/addBalance.vue";
import type { topUp } from "./types";
import { type Ref, reactive, ref, onMounted, watch, h, toRaw } from "vue";
import { addDialog } from "@/components/ReDialog";
import { deviceDetection } from "@pureadmin/utils";
import { message } from "@/utils/message";
// refreshCallback?: () => void
export function useRole(tableRef: Ref, treeRef: Ref) {
  const form = reactive({
    customerName: "",
    shortName: "",
    mobile: "",
    remark: ""
  });
  const formRef = ref();
  const curRow = ref();
  const dataList = ref([]);
  const treeIds = ref([]);
  const treeData = ref([]);
  const isShow = ref(false);
  const loading = ref(false);
  const isLinkage = ref(false);
  const treeSearchValue = ref();
  const isExpandAll = ref(false);
  const isSelectAll = ref(false);
  const selectedNum = ref(0);
  const treeProps = {
    value: "id",
    label: "title",
    children: "children"
  };

  const userInfo = ref({
    company: "",
    name: "",
    count: 0,
    depositedAmount: 0
  });

  // 拿首页所有的数据
  async function GetUserInfo() {
    loading.value = true;
    setTimeout(() => {
      loading.value = false;
    }, 500);
    const { data } = await getUserInfo({
      userId: localStorage.getItem("userId")
    });
    console.log("首页拿到的个人信息", data);
    userInfo.value = data;
  }
  async function addBalance() {
    addDialog({
      title: `余额充值`,
      props: {
        formInline: {
          id: Number(localStorage.getItem("userId")),
          depositedAmount: "25"
        }
      },
      width: "40%",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      hideFooter: true,
      closeOnClickModal: false,
      contentRenderer: () =>
        h(addBalanceForm, { ref: formRef, formInline: null })
      // beforeSure: (done, { options }) => {
      //   const FormRef = formRef.value.getRef();
      //   const curData = options.props.formInline as topUp;
      //   function chores() {
      //     message(`完成!`, {
      //       type: "success"
      //     });
      //     done(); // 关闭弹框
      //     // GetUserInfo(); // 刷新表格数据
      //   }
      //   FormRef.validate(async valid => {
      //     if (valid) {
      //       // 表单规则校验通过
      //       curData.depositedAmount = Number(curData.depositedAmount);
      //       await addRecharge(toRaw(curData));

      //       chores();
      //     }
      //   });
      // }
    });
  }
  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    GetUserInfo();
  };

  const filterMethod = (query: string, node) => {
    return transformI18n(node.title)!.includes(query);
  };

  onMounted(async () => {
    // GetUserInfo();
  });

  watch(isExpandAll, val => {
    val
      ? treeRef.value.setExpandedKeys(treeIds.value)
      : treeRef.value.setExpandedKeys([]);
  });

  watch(isSelectAll, val => {
    val
      ? treeRef.value.setCheckedKeys(treeIds.value)
      : treeRef.value.setCheckedKeys([]);
  });

  return {
    form,
    isShow,
    curRow,
    loading,
    dataList,
    treeData,
    selectedNum,
    treeProps,
    isLinkage,
    isExpandAll,
    isSelectAll,
    treeSearchValue,
    GetUserInfo,
    resetForm,
    filterMethod,
    transformI18n,
    userInfo,
    addBalance
  };
}
